package util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Window;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import coom.DownloadService;
import widget.alertview.AlertView;
import widget.alertview.OnItemClickListener;


/**
 * Created by admin on 2017/8/4.
 * <p>
 * 弹框设置
 */
public class AlertUtils {
    public interface AlertViewControl {
        void onItemClickListener(int position);
    }

    public static TimePickerDialog timePicker(Context context, int before, int after, OnDateSetListener listener) {
        return new TimePickerDialog.Builder()
                .setCallBack(listener)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - 3600L * 1000 * 24 * before)
                .setMaxMillseconds(System.currentTimeMillis() + 3600L * 1000 * 24 * after)
                .setCurrentMillseconds(System.currentTimeMillis()) //设置选择时间
                .setThemeColor(context.getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(context.getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(context.getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();
    }

    public static TimePickerDialog timePicker2(Context context, int before, int after, long dataTime, OnDateSetListener listener) {
        return new TimePickerDialog.Builder()
                .setCallBack(listener)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - 3600L * 1000 * 24 * before)
                .setMaxMillseconds(System.currentTimeMillis() + 3600L * 1000 * 24 * after)
                .setCurrentMillseconds(dataTime)
                .setThemeColor(context.getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.YEAR_MONTH_DAY)
                .setWheelItemTextNormalColor(context.getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(context.getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();
    }

    public static TimePickerDialog timePicker3(Context context, int before, int after, long dataTime, OnDateSetListener listener) {
        return new TimePickerDialog.Builder()
                .setCallBack(listener)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
//                .setDayText("日")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - 3600L * 1000 * 24 * before)
                .setMaxMillseconds(System.currentTimeMillis() + 3600L * 1000 * 24 * after)
                .setCurrentMillseconds(dataTime)
                .setThemeColor(context.getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.YEAR_MONTH)
                .setWheelItemTextNormalColor(context.getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(context.getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();
    }

    public static TimePickerDialog timePicker4(Context context, int before, int after, long dataTime, OnDateSetListener listener) {
        return new TimePickerDialog.Builder()
                .setCallBack(listener)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - 3600L * 1000 * 24 * before)
                .setMaxMillseconds(System.currentTimeMillis() + 3600L * 1000 * 24 * after)
                .setCurrentMillseconds(dataTime)
                .setThemeColor(context.getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.YEAR_MONTH_DAY)
                .setWheelItemTextNormalColor(context.getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(context.getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();
    }

//    public static AlertView photoPicker(final Context context, final FunctionConfig functionConfig,
//                                        final GalleryFinal.OnHanlderResultCallback callback) {
//        return new AlertView.Builder().setContext(context)
//                .setStyle(AlertView.Style.ActionSheet)
//                .setTitle("选择操作")
//                .setMessage(null)
//                .setCancelText("取消")
//                .setDestructive("拍照", "从相册中选择")
//                .setOthers(null)
//                .setOnItemClickListener(new OnItemClickListener() {
//                    @Override
//                    public void onItemClick(Object o, int position) {
//                        String path = "";
//                        switch (position) {
//                            case 0:
//                                GalleryFinal.openCamera(Constants.REQUEST_CODE_CAMERA, functionConfig, callback);
//                                break;
//                            case 1:
////                                if (mutiSelect) {
//                                GalleryFinal.openGalleryMuti(Constants.REQUEST_CODE_GALLERY, functionConfig, callback);
////                                } else {
////                                  GalleryFinal.openGallerySingle(Constants.REQUEST_CODE_GALLERY, functionConfig, callback);
////                                }
//                                break;
////                            case 2:
////                                if (new File(path).exists()) {
////                                    GalleryFinal.openCrop(Constants.REQUEST_CODE_CROP, functionConfig, path, callback);
////                                } else {
////                                    Toast.makeText(context, "图片不存在", Toast.LENGTH_SHORT).show();
////                                }
////                                break;
////                            case 3:
////                                if (new File(path).exists()) {
////                                    GalleryFinal.openEdit(Constants.REQUEST_CODE_EDIT, functionConfig, path, callback);
////                                } else {
////                                    Toast.makeText(context, "图片不存在", Toast.LENGTH_SHORT).show();
////                                }
////                                break;
//                            default:
//                                break;
//                        }
//                    }
//                })
//                .build();
//    }


    /**
     * 0-订单，提示框
     */
    public static AlertView checkInfo(final Context context, final AlertViewControl control, String info, String[] str) {
        return new AlertView.Builder().setContext(context)
                .setStyle(AlertView.Style.ActionSheet)
                .setTitle(info)
                .setMessage(null)
                .setCancelText("取消")
                .setDestructive(str)
                .setOthers(null)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        control.onItemClickListener(position);
                    }
                })
                .build();
    }

    /**
     * 加载框
     */
    public static AlertDialog loadingDialog(Context context) {
        AlertDialog dialog = new AlertDialog.Builder(context, R.style.NoBackgroundDialog).create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);//按对话框以外的地方不起作用。按返回键还起作用
//        dialog.setCancelable(false);//按对话框以外的地方不起作用。按返回键也不起作用
        dialog.setContentView(LayoutInflater.from(context).inflate(R.layout.dialog_loading, null));
        return dialog;
    }

    /**
     * 退出或者操作提示框
     * <p>
     * 确定 position为0
     */

    public static AlertView showInfo(final Context context, final AlertViewControl control, String title, String info) {
        return new AlertView.Builder().setContext(context)
                .setStyle(AlertView.Style.Alert)
                .setTitle(title)
                .setMessage(info)
                .setDestructive("确定")
                .setCancelText("取消")
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        control.onItemClickListener(position);
                    }
                })
                .build();
    }

    /**
     * 单步操作提示框
     * <p>
     * 确定 position为0
     */

    public static AlertView showSigleInfo(final Context context, final AlertViewControl control, String title, String info) {
        return new AlertView.Builder().setContext(context)
                .setStyle(AlertView.Style.Alert)
                .setTitle(title)
                .setMessage(info)
                .setDestructive("确定")
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        control.onItemClickListener(position);
                    }
                })
                .build();
    }


    public static AlertDialog updateDialog(final Context context, final String url) {
        return new AlertDialog.Builder(context, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
                .setTitle("发现新版本")
                .setMessage("发现可用的新版本,是否立即更新?")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ToastUtils.shortToast("开始下载...");
                        Intent intent = new Intent(context, DownloadService.class);
                        intent.putExtra("url", url);
                        context.startService(intent);
                    }
                })
                .create();
    }

//    AlertView 拓展用法
//    //拓展窗口
//    mAlertViewExt = new AlertView("提示", "请完善你的个人资料！", "取消", null, new String[]{"完成"}, this, AlertView.Style.Alert, this);
//    ViewGroup extView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.alertext_form,null);
//    etName = (EditText) extView.findViewById(R.id.etName);
//        etName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//        @Override
//        public void onFocusChange(View view, boolean focus) {
//            //输入框出来则往上移动
//            boolean isOpen=imm.isActive();
//            mAlertViewExt.setMarginBottom(isOpen&&focus ? 120 :0);
//            System.out.println(isOpen);
//        }
//    });
//        mAlertViewExt.addExtView(extView);
}